package com.redoor.app.stake.configurer;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 多数据源配置
 * Created by Floki on 2017/11/10.
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DruidDataSourceConfigurer {
    private static Logger log = LoggerFactory.getLogger(DruidDataSourceConfigurer.class);

    /**
     * 写库 数据源配置
     * @return
     */
    @Primary
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "mysql.datasource.write")
    public DataSource masterDataSource() {
        log.info("-------------------- masterDataSource init ---------------------");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "slaveDataSource01")
    @ConfigurationProperties(prefix = "mysql.datasource.read01")
    public DataSource slaveDataSource01() {
        log.info("-------------------- slaveDataSource01 init ---------------------");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "slaveDataSource02")
    @ConfigurationProperties(prefix = "mysql.datasource.read02")
    public DataSource slaveDataSource02() {
        log.info("-------------------- slaveDataSource02 init ---------------------");
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 注册ServletRegistrationBean
     * @return
     */
    @Bean
    public ServletRegistrationBean registrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        /** 初始化参数配置，initParams**/
        //白名单
        bean.addInitParameter("allow", "127.0.0.1");
        //IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        bean.addInitParameter("deny", "192.168.1.73");
        //登录查看信息的账号密码.
        bean.addInitParameter("loginUsername", "admin");
        bean.addInitParameter("loginPassword", "123");
        //是否能够重置数据.
        bean.addInitParameter("resetEnable", "false");
        return bean;
    }

    /**
     * 注册FilterRegistrationBean
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        bean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return bean;
    }

}
